<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

        function __construct()
        {
                parent::__construct();
                $this->load->database();
                $this->load->helper('url');
                $this->load->library('session');
                $this->load->library('table');
                $this->load->helper(array('form', 'url'));
        }
        function index()
        {
                log_message('debug', "index page loaded, Welcome!");
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        redirect(site_url('welcome/admin_cms/'), 'refresh ', 302);
                }
                else {
                        $name = $this->session->userdata('name');
                        if($name!='')
                        {
                                redirect(site_url('welcome/upload_cms/'), 'refresh ', 302);
                        }
                        else
                        {
                                $scan_name = $this->session->userdata('scan_name');
                                if($scan_name!='')
                                {
                                        redirect(site_url('scan_admin/scan_cms/'), 'refresh ', 302);
                                }
                                else
                                {
                                        $this->load->view('login');
                                }
                        }
                }
        }
        function check_login()
        {
                log_message('debug', "check login page loaded, Welcome!");
                $this->load->model('admin_login');
                $login=$this->admin_login->get_post();
                if ($login==false)
                {
                        $this->load->view('login_fail');
                }
        }
        function top()
        {
                $admin['name']=$this->session->userdata('admin_name');
                $this->load->view('top',$admin);
        }
        function center()
        {
                $this->load->view('center');
        }
        function bottom()
        {
                $this->load->view('bottom');
        }
        function left()
        {
                $sign_name['sign']=$this->session->userdata('admin_name');
                $this->load->view('left',$sign_name);
        }
        function right()
        {
                $this->load->view('right');
        }
        function admin_cms()
        {
                $sign_name['sign']=$this->session->userdata('admin_name');
                if($sign_name['sign']!='')
                {
                $this->load->view('admin_cms',$sign_name);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function add_category()
        {
                $data['sign']=$this->session->userdata('admin_name');
                $this->load->view('add_category',$data);
        }
        function add_second_category()
        {
                $data['sign']=$this->session->userdata('admin_name');
                $this->load->model('manage_books');
                $cate=$this->manage_books->get_category();
                if($cate!=FALSE)
                {
                        $data['cate']=$cate;
                        $data['error']='';
                        $data['sign']=$this->session->userdata('admin_name');
                        $this->load->view('second_cate',$data);
                }
                else
                {
                        echo "You have not added first category!";
                }

        }
        function insert_category()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                    $this->admin_login->insert_category();
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function insert_second_category()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                        $result=$this->admin_login->insert_second_category();
                        if($result==true)
                        {
                                $msg['title']="Second Category";
                                $msg['redirect']="welcome/add_second_category";
                                $data['msg']=$msg;
                                $this->load->view('add_success',$data);
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_pdf($sid=0)
        {
                $this->load->model('manage_books');
                $cate=$this->manage_books->get_category();
                if($cate!=FALSE)
                {
                        $data['cate']=$cate;
                        $data['error']='';
                        $data['sign']=$this->session->userdata('admin_name');
                        $data['sid'] = $sid;
                        if ($sid > 0) {
                                $se_book = $this->manage_books->get_book_message($sid);
                                $data['se_name'] = $se_book['bn'];
                        }
                        $this->load->view('upload_setp_one',$data);
                }
                else
                {
                        echo "You have not added books category!";
                }
        }

        function manage_pdf()
        {
                $this->load->view('manage_pdf');
        }
        function add_admin()
        {
                $data['sign']=$this->session->userdata('admin_name');
                $this->load->view('add_admin',$data);
        }
        function manage_admin()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                        $result['tr']=$this->admin_login->select_admin();
                        $result['sign']=$this->session->userdata('admin_name');
                        if($result['tr']!=false)
                        {
                                $this->load->view('manage_admin',$result);
                        }
                        else
                        {
                                echo 'No Account Records!';
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function insert_admin()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                        $this->admin_login->insert_admin();
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function destory()
        {
                $this->session->sess_destroy();
                redirect(site_url('welcome/index/'), 'refresh ', 302);
        }
        function destory_name()
        {
                $this->session->unset_userdata('name');
                redirect(site_url('welcome/index/'), 'refresh ', 302);
        }
        function update($uid)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                        $result['row']=$this->admin_login->select_message($uid);
                        $result['sign']=$this->session->userdata('admin_name');
                        $this->load->view('edit_admin',$result);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upadte_admin()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                        $this->admin_login->update_admin();
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function delete($uid)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_login');
                        $this->admin_login->delete_admin($uid);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function search_page()
        {
                $key=$this->input->post('key');
                $this->search($key,$offset='0');
        }
        function search($key,$offset='0')
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                $limit=30;
                $this->load->model('manage_books');
                $this->load->library('pagination');
                $config['base_url'] = site_url('welcome/search'.'/'.$key).'/';
                $config['total_rows'] = $this->manage_books->search_num($key);
                $data['page_result']=$this->manage_books->search($limit,$offset,$key);
                $config['uri_segment'] = 4;
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['key']=$key;
                if($config['total_rows']==1)
                {
                        $data['start']=0;
                }
                else
                {
                        if($offset==$config['total_rows']-1)
                        {
                                $data['start']=$offset-$limit;
                        }
                        else
                        {
                                $data['start']=$offset;
                        }
                }
                $data['page_link']=$this->pagination->create_links();
                $data['sign']=$this->session->userdata('admin_name');
                $this->load->view('search_result',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function del_search_book($key,$offset,$b_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        if($this->manage_books->del_book($b_id))
                        {
                                        redirect(site_url('welcome/search'.'/'.$key.'/'.$offset), 'refresh ', 302);
                        }
                        else
                        {
                                        echo "Delete Fail";
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_cms()
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $cate=$this->manage_books->get_category();
                        if($cate!=FALSE)
                        {
                                $data['cate']=$cate;
                                $sign['sign']=$this->session->userdata('name');
                                $this->load->view('comm_cms_head',$sign);
                                $this->load->view('upload_basic_msg',$data);
                                $this->load->view('comm_cms_foot');
                        }
                        else
                        {
                                echo "Super Admin have not added books category!";
                        }

                }
                else
                {
                        $this->load->view('return');
                }
        }
        function insert_basic_msg()
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $b_id=$this->manage_books->upload_setp_one();
                        if($b_id!=false)
                        {
                                redirect(site_url('welcome/upload_book_cover'.'/'.$b_id), 'refresh ', 302);
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_book_cover($b_id)
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                $sign['sign']=$this->session->userdata('name');
                $this->load->view('comm_cms_head',$sign);
                $this->load->view('upload_book_cover', array('error' => ' ','b_id'=>$b_id));
                $this->load->view('comm_cms_foot');
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function edit_book($b_id)
        {
                $this->load->model('manage_books');
                $admin_name = $this->session->userdata('name');
                $cate=$this->manage_books->get_category();
                if($admin_name!='')
                {
                        if($cate!=FALSE)
                        {
                                $data['cate']=$cate;
                                $data['b_id']=$b_id;
                                $data['msg']=$this->manage_books->get_book_message($b_id);
                                $slave=$this->manage_books->get_book_slave($b_id);
                                if(!empty($slave))
                                {
                                        $data['se_cate']=$this->manage_books->get_second_category($slave[0]['type_id']);
                                        $data['slave']=$slave;
                                }
                                //print_r($se_cate);
                                $sign['sign']=$this->session->userdata('name');
                                $this->load->view('comm_cms_head',$sign);
                                $this->load->view('edit_book_basic',$data);
                                $this->load->view('comm_cms_foot');
                        }
                        else
                        {
                                echo "You have not added books category!";
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_plist($b_id)
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                        $data['b_id']=$b_id;
                        $sign['sign']=$this->session->userdata('name');
                        $this->load->view('comm_cms_head',$sign);
                        $this->load->view('upload_book_plist',$data);
                        $this->load->view('comm_cms_foot');
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_book($b_id)
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                        set_time_limit(0);
                        $data['b_id']=$b_id;
                        $sign['sign']=$this->session->userdata('name');
                        $this->load->view('comm_cms_head',$sign);
                        $this->load->view('upload_book_content',$data);
                        $this->load->view('comm_cms_foot');
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_setp_two($b_id)
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                          $config['upload_path'] = 'SharePic/';
                          $config['allowed_types'] = 'gif|jpg|png';
                          $config['max_size'] = '100';
                          $config['max_width']  = '1024';
                          $config['max_height']  = '768';
                          $config['file_name']  = date("YmdHis").rand(10000,99999);
                          $this->load->library('upload', $config);
                          if ( ! $this->upload->do_upload())
                          {
                           $error = array('error' => $this->upload->display_errors(),'b_id'=>$b_id);
                           $sign['sign']=$this->session->userdata('name');
                           $this->load->view('comm_cms_head',$sign);
                           $this->load->view('upload_book_cover', $error);
                           $this->load->view('comm_cms_foot');
                          }
                          else
                          {
                                $data =  $this->upload->data();
                                $url=$config['upload_path'].$data['file_name'];
                                $sql="update `books` set `pic`='$url' where `b_id`='$b_id'";
                                if($this->db->query($sql))
                                {
                                        redirect(site_url('welcome/upload_plist'.'/'.$b_id), 'refresh ', 302);
                                }
                          }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function update_book_basic($b_id)
        {
                $admin_name = $this->session->userdata('name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $b_id=$this->manage_books->update_book_basic($b_id);
                        if($b_id!=false)
                        {
                                redirect(site_url('welcome/upload_book_cover'.'/'.$b_id), 'refresh ', 302);
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function add_scan_admin()
        {
                $data['sign']=$this->session->userdata('admin_name');
                $this->load->view('add_scan_admin',$data);
        }
        function insert_scan_admin()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_manage');
                        $this->admin_manage->insert_scan_admin();
                }
                else
                {
                        $this->load->view('return');
                }
        }
}

























